var mydialog;

$(function(){
	$.ajax({
		type:'get',
		url:'contract',
		dataType:'json',
		data:{operator:'9',load:true},
		success:loadBookData
	});
});

function loadBookData(data){
	$("#book_table").html("<tr id='load_header'><th width='4%'></th><th title='可搜尋'>起始號碼</th><th title='可搜尋'>終止號碼</th><th>到店日</th><th title='可搜尋'>使用店秘書</th><th>領取份數</th><th>使用份數</th><th>作廢份數</th> <th>遺失份數</th><th>可用份數</th><th>使用率</th></tr>");
	if(data.data!=null){
		$.each(data.data,function(key,value){
			$("#book_table").append("<tr align='center' class='book_tr'><td class='book_td'><input type='radio' name='book' value='"+value['group_id']+"' class='check'/></td><td class='book_td'>"+value['start_num']+"</td><td class='book_td'>"+value['end_num']+"</td><td class='book_td'>"+value['date']+"</td><td class='book_td'>"+value["who"]+"</td><td class='book_td'>"+value["Rnum"]+"</td><td class='book_td'>"+value['Usenum']+"</td><td class='book_td'>"+value["vnum"]+"</td><td class='book_td'>"+value["lnum"]+"</td><td class='book_td'>"+value['isuse']+"</td><td class='book_td'>"+value['useL']+"%</td></tr>");
		});
	}else{
		$("#book_table").append("<tr colspan='11'>暫時沒有合約書哦</tr>");
	}
}

function new_book(){
	mydialog=art.dialog({
		title:'新建合約',
		width:535,
		height:500,
		lock:true
	});
	$.ajax({
		type:'post',
		url:'new_book',
		dataType:'html',
		success:function(data){
			mydialog.content(data);
	}
	});
}

function checkform_newbook(){
	var startnum=$("#start_booknum").val();
	var endnum=$("#end_booknum").val();
	var staff=$("#staff").val();
	var book_type=$("#book_type").val();
	if(startnum===''){
		alert('起始編號不能為空');
		return;
	}else if(endnum===''){
		alert('結束編號不能為空');
		return ;
	}
	var re=/([A-Z]*)(\d{6})$/;
	if(re.test(startnum)){
		if(re.test(endnum)){
			var data=re.exec(startnum);
			var data2=re.exec(endnum);
			var str1=data[1];
			var num1=data[2];
			var str2=data2[1];
			var num2=data2[2];
			if(str1===str2){
				var re=/[0]*([1-9][0-9]*)/;
				if(!re.test(num1)||!re.test(num2)){
					alert('數字編號不能全部額為0');
					return;
				}
				var data=re.exec(num1);
				var data2=re.exec(num2);
				var num11=data[1];
				var num22=data2[1];
				if(num22>100){
					alert('數字編號不能大於100');
					return;
				}else if(num22<num11){
					alert('終止編號不能小於起始編號');
					return ;
				}else if(num22>num11){
					mydialog.content("提交中....");
					$.ajax({
						type:'post',
						url:'contract_save',
						dataType:'json',
						data:{start_str:str1,start_num:num11,end_num:num22,who:staff,book_type:book_type,operator:'9'},
						success:newbook_status
					});
				}
			}else{
				alert("你兩次輸入的編號字母不一致");
				return;
			}
		}else{
			alert('你輸入的終止號碼格式不對');
			return;
		}
	}else{
		alert('你輸入的起始編號格式不對');
		return;
	}
}

function newbook_status(data){
	mydialog.close();
	if(data.status===0){
		art.dialog({lock:true,title:'提示',content:'没有权限',ok:function(){}});
	}else if(data.status===2){
		art.dialog({lock:true,title:'提示',content:'非法訪問',ok:function(){}});
	}else if(data.status===3){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){}});
	}else if(data.status===1){
		art.dialog({lock:true,title:'提示',width:300,height:100,content:'成功',ok:function(){
			location.reload();
		}});
	}
}
var value="";
function book_alter(){
	var check=$(".check").get();
	var ischeck=0;
	
	$.each(check,function(key,values){
		if(values["checked"]){
			value=values["value"];
			ischeck++;
		}
	});
	if(ischeck===1){
		mydialog=art.dialog({title:'請稍候',lock:true});
		$.ajax({
			type:'post',
			url:'contract_alter',
			dataType:'json',
			data:{group_id:value,operator:'20'},
			success:book_alter_status
		});
	}else{
		alert('請先選擇要修改的條目');
		return;
	}
	
}

function book_alter_status(data){
	mydialog.close();
	if(data.status===0){
		art.dialog({lock:true,title:'提示',content:'没有权限',ok:function(){}});
	}else if(data.status===2){
		art.dialog({lock:true,title:'提示',content:'非法訪問',ok:function(){}});
	}else if(data.status===3){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){}});
	}else if(data.status===1){
		var table="<div id='results'></div>";
		$("#book_alter_content").html(table);
		$("#book_alter_content #results").append("<div class='alter_header'><span>選擇</span><span>合約類別</span><span>領取日期</span><span>使用日期</span><span>領用人</span><span>持有狀態</span><span>備註</span><span>操作</span></div>");
		var i=0;
		$.each(data.data,function(key,value){
			var status="";
			if(value["isUse"]==='0'){
				status="未使用";
			}else{
				status="已使用";
			}
			if(value["isValid"]==='1'){
				status="已作廢";
			}else{
				if(value["isLost"]==='1'){
					status="已遺失";
				}
			}
			var r_date=value["r_date"];
			if(r_date=='0000-00-00'){
				r_date="";
			}
			$("#book_alter_content #results").append("<div class=alter"+i+"><span><input type='radio' name='alter_book'/></span><span>"+value["book_num"]+"</span><span>"+r_date+"</span><span>"+value["use_date"]+"</span><span>"+value["who_use"]+"</span><span>"+status+"</span><span>"+value["remark"]+"</span><span><input type='button' value='編輯' rel='"+value["id"]+"' bookname="+value["book_num"]+" lost="+value["isLost"]+" valid="+value["isValid"]+" onclick='book_edit(this);' /></span></div>");
			if(i%2==0){
				$('.alter'+i).css("background-color","#CBE1FC");
			}
			$('.alter'+i).hover(function(){
				style=$(this).css("background-color");
				$(this).css("background-color","#FFFF99");
			},function(){
				$(this).css("background-color",style);
			});
			i++;
		});
		$("#book_alter").css("display","block");
	}
}

var id=0;
var bookname="";
var isLost=false;
var isInvalid=false;

function book_edit(obj){
	id=$(obj).attr("rel");
	bookname=$(obj).attr("bookname");
	var temp=$(obj).attr("lost");
	if(temp==='1'){
		isInvalid=true;
	}
	temp=$(obj).attr("valid");
	if(temp==='1'){
		isLost=true;
	}
	$.ajax({
		type:'post',
		url:'book_alter_html',
		dataType:'html',
		success:book_edit_html,
		error:function(){alert("error");}
	});
}

function book_edit_html(data){
	mydialog=art.dialog({title:bookname,content:data,lock:true});
	 $( "#r_date" ).datepicker({
		 dateFormat:"yy-mm-dd",
		 changeMonth:true,
		 changeYear:true
	 });
}

function book_r(){
	if(isLost||isInvalid){
		alert("此状态无法领取");
		return;
	}
	var jjr=$("#jjr").val();
	var r_date=$("#r_date").val();
	var remark=$("#remark").val();
	if(jjr===''){
		alert('經紀人不能為空');
		return;
	}
	if(confirm("確定領取嗎？")){
		mydialog.content("處理中...");
		
		$.ajax({
			type:'post',
			url:'contract_alter_save',
			dataType:'json',
			data:{jjr:jjr,r_date:r_date,id:id,type:1,remark:remark,operator:'20'},
			success:book_alter_save_status
		});
	}
}

function book_alter_save_status(data){
	mydialog.close();
	if(data.status===0){
		art.dialog({lock:true,title:'提示',content:'没有权限',ok:function(){}});
	}else if(data.status===2){
		art.dialog({lock:true,title:'提示',content:'非法訪問',ok:function(){}});
	}else if(data.status===3){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){}});
	}else if(data.status===1){
		art.dialog({lock:true,title:'提示',width:300,height:100,content:data.info,ok:function(){
			$.ajax({
				type:'post',
				url:'contract_alter',
				dataType:'json',
				data:{group_id:value,operator:'20'},
				success:book_alter_status
			});
			$.ajax({
				type:'get',
				url:'contract',
				dataType:'json',
				data:{operator:'9',load:true},
				success:loadBookData
			});
		}});
	}else if(data.status===5){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){
			location.reload();
		}});
	}
}

function book_reset(){

	if(confirm("確定要還原嗎？")){
		mydialog.content("處理中...");
		
		$.ajax({
			type:'post',
			url:'contract_alter_save',
			dataType:'json',
			data:{id:id,type:4,operator:'20'},
			success:book_alter_save_status
		});
	}
}

function book_invalid(){
	if(isLost){
		alert("已經遺失，作廢無效");
		return;
	}
	if(isInvalid){
		alert("已經作廢，不能重複操作");
		return;
	}
	if(confirm("確定要作廢嗎？")){
		mydialog.content("處理中...");
		
		$.ajax({
			type:'post',
			url:'contract_alter_save',
			dataType:'json',
			data:{id:id,type:2,operator:'20'},
			success:book_alter_save_status
		});
	}
}

function book_lost(){
	if(isInvalid){
		alert("已经作废，遗失无效");
		return;
	}
	if(isLost){
		alert("已經遺失，不能重複操作");
		return;
	}
	if(confirm("確定丟失嗎？")){
		mydialog.content("處理中...");
		
		$.ajax({
			type:'post',
			url:'contract_alter_save',
			dataType:'json',
			data:{id:id,type:3,operator:'20'},
			success:book_alter_save_status
		});
	}
}

function book_del(){
	var check=$(".check").get();
	var ischeck=0;
	
	$.each(check,function(key,values){
		if(values["checked"]){
			value=values["value"];
			ischeck++;
		}
	});
	if(ischeck===1){
		if(confirm("確定刪除嗎？")){
			mydialog=art.dialog({title:'請稍候',lock:true});
			$.ajax({
				type:'post',
				url:'contract_alter_save',
				dataType:'json',
				data:{type:5,operator:'20',group_id:value},
				success:book_alter_save_status
			});
		}
	}else{
		alert('請先選擇要刪除的條目');
		return;
	}
}